<?php

namespace App\Kuafu\Common\Service;


use App\Library\KfHttp\TigerCurl;
use Illuminate\Support\Facades\Log;
use App\Kuafu\Shop\Model\Shop;

class InformationPushService
{
    /**
     * 消息推送服务
     */

    const KF_OPEN_PUSH          = 1;
    const MT_OPEN_PUSH          = 2;
    const ELM_OPEN_PUSH         = 3;
    const DY_GROUP_OPEN_PUSH    = 4;
    const MT_GROUP_OPEN_PUSH    = 5;
    const ALY_MONITOR_OPEN_PUSH = 6;


    //通知标题
    const NOTIFY_TITLE_MAPPING = [
        self::KF_OPEN_PUSH          => '功能开通成功提醒',
        self::MT_OPEN_PUSH          => '功能开通成功提醒',
        self::ELM_OPEN_PUSH         => '功能开通成功提醒',
        self::DY_GROUP_OPEN_PUSH    => '功能开通成功提醒',
        self::MT_GROUP_OPEN_PUSH    => '功能开通成功提醒',
        self::ALY_MONITOR_OPEN_PUSH => '功能开通成功提醒',
    ];

    //通知内容
    const NOTIFY_CONTENT_MAPPING = [
        self::KF_OPEN_PUSH          => '尊敬的夸父伙伴您好，%s夸父收银支付账号已申请并绑定成功，可在夸父收银通过扫码方式进行收款下单或夸父点餐小程序在线下单。',
        self::MT_OPEN_PUSH          => '尊敬的夸父伙伴您好，%s美团外卖账号已申请并绑定成功，可在夸父收银自动接收外卖订单并打印。',
        self::ELM_OPEN_PUSH         => '尊敬的夸父伙伴您好，%s饿了么外卖账号已申请并绑定成功，可在夸父收银自动接收外卖订单并打印。',
        self::DY_GROUP_OPEN_PUSH    => '尊敬的夸父伙伴您好，%s抖音团购账号已申请并绑定成功，可在夸父收银点单-更多-抖音团购优惠券进行扫码核销。',
        self::MT_GROUP_OPEN_PUSH    => '尊敬的夸父伙伴您好，%s美团点评团购账号已申请并绑定成功，可在夸父收银点单-更多-美团点评优惠券进行扫码核销。',
        self::ALY_MONITOR_OPEN_PUSH => '尊敬的夸父伙伴您好，%s阿里云监控账号已申请并绑定成功，可在夸父商家-门店(工作台)-工具箱-云监控 查看监控摄像记录。',
    ];

    // 通知位置
    const NOTICE_PRESENCE_AREA_1 = 1;
    const NOTICE_PRESENCE_AREA_2 = 2;
    const NOTICE_PRESENCE_AREA_3 = 3;
    const NOTICE_PRESENCE_AREA_4 = 4;


    const NOTIFY_PRESENCE_AREA_MAPPING = [
        self::NOTICE_PRESENCE_AREA_1 => '首页',
        self::NOTICE_PRESENCE_AREA_2 => '订货',
        self::NOTICE_PRESENCE_AREA_3 => '我的',
        self::NOTICE_PRESENCE_AREA_4 => '门店',
    ];

    // 是否系统生成
    const NOTIFY_IS_SYSTEM_NO  = 0;
    const NOTIFY_IS_SYSTEM_YES = 1;

    const IS_POPUP_ON  = 1;  //是否弹窗  是
    const IS_POPUP_OFF = 0;  // 否


    const METHOD_TYPE_NOT_SET   = 0;   //未设置
    const METHOD_TYPE_MANUAL    = 1;   //手动
    const METHOD_TYPE_AUTOMATIC = 2;   //自动

    // 通知对象
    const NOTIFY_TARGET_1 = 1;
    const NOTIFY_TARGET_2 = 2;
    const NOTIFY_TARGET_3 = 3;

    const NOTIFY_TARGET_MAPPING = [
        self::NOTIFY_TARGET_1 => '店长',
        self::NOTIFY_TARGET_2 => '所有人',
        self::NOTIFY_TARGET_3 => '个人账号',
    ];

    //公告分类
    const NOTIFY_TYPE_1 = 1;
    const NOTIFY_TYPE_2 = 2;
    const NOTIFY_TYPE_3 = 3;
    const NOTIFY_TYPE_4 = 4;
    const NOTIFY_TYPE_5 = 5;

    const NOTIFY_TYPE_MAPPING = [
        self::NOTIFY_TYPE_1 => '订货消息',
        self::NOTIFY_TYPE_2 => '服务消息',
        self::NOTIFY_TYPE_3 => '待办消息',
        self::NOTIFY_TYPE_4 => '开店进度',
        self::NOTIFY_TYPE_5 => '门店消息',
    ];

    public static function push($type, $userIds, Shop $shopModel = null)
    {
        try {
            switch ($type) {
                case self::KF_OPEN_PUSH:
                case self::MT_OPEN_PUSH:
                case self::ELM_OPEN_PUSH:
                case self::DY_GROUP_OPEN_PUSH:
                case self::MT_GROUP_OPEN_PUSH:
                case self::ALY_MONITOR_OPEN_PUSH:
                    $params = self::setFunctionOpenParams($type, $userIds, $shopModel);
                    break;
                default:
                    return;
            }
            TigerCurl::getIns()->do('common/information/appPush', $params, 'POST');
            Log::info('发送商家APP消息通知成功-参数' . json_encode($params) . '用户ID' . json_encode($userIds));
        } catch (\Exception $exception) {
            Log::info('发送商家APP消息通知 失败！   参数' . json_encode($params) . '用户ID' . json_encode($userIds));
        }
    }

    protected static function setFunctionOpenParams($type, $userIds, Shop $shopModel = null): array
    {
        $getTitle = function () use ($type, $shopModel) {
            return self::NOTIFY_TITLE_MAPPING[$type] ?? "功能开通成功提醒";
        };

        $getSummary = function () use ($type, $shopModel) {
            if (isset(self::NOTIFY_CONTENT_MAPPING[$type])) {
                return vsprintf(
                    self::NOTIFY_CONTENT_MAPPING[$type],
                    [
                        $shopModel ? "{$shopModel->shopCode}({$shopModel->name})" : ""
                    ]
                );
            } else {
                return "您申请的功能已开通成功";
            }
        };

        return [
            "title"        => $getTitle(),
            "notifyType"   => self::NOTIFY_TYPE_5,
            "target"       => self::NOTIFY_TARGET_1,
            "sms"          => "",
            "url"          => "",
            "methodType"   => self::METHOD_TYPE_AUTOMATIC,
            "summary"      => $getSummary(),
            "contents"     => $getSummary(),
            "isPopup"      => self::IS_POPUP_ON,
            "presenceArea" => self::NOTICE_PRESENCE_AREA_1,
            "pushInfo"     => $userIds,
            "shopId"       => $shopModel ? $shopModel->id : 0,
        ];
    }

}
